﻿using System;
using System.Collections;
using System.IO;
using System.Runtime.Serialization;
using System.Security;

namespace Eflong.Structure
{
    public class RecordManager
    {
        /// <summary>
        /// 从记录文件中，获取记录集合
        /// </summary>
        /// <param name="recordFileName"></param>
        /// <returns></returns>
        public static ArrayList LoadRecords( string recordFileName )
        {
            ArrayList records = new ArrayList( );
            FileStream recordsFile = null;
            if ( File.Exists( recordFileName ) )
            {
                try
                {
                    recordsFile = new FileStream( recordFileName , FileMode.Open , FileAccess.Read , FileShare.None );
                }
                catch ( IOException )
                {
                }
                catch ( SecurityException )
                {
                }
                catch ( UnauthorizedAccessException )
                {
                }
            }
            if ( recordsFile != null )
            {
                //IFormatter formatter = new BinaryFormatter();
                bool flag = false;
                try
                {
                    while ( recordsFile.Position < recordsFile.Length )
                    {
                        LoadMultiRecord( records , recordsFile , 1000 );
                    }
                }
                catch ( IOException )
                {
                }
                catch ( SecurityException )
                {
                }
                catch ( UnauthorizedAccessException )
                {
                }
                catch ( SerializationException ex )
                {
                    Console.WriteLine( ex.Message );
                    flag = true;
                }

                recordsFile.Close( );
            }
            return records;
        }

        /// <summary>
        /// 从文件流中，一次获取一批记录
        /// </summary>
        /// <param name="records"></param>
        /// <param name="recordsFile"></param>
        /// <param name="count"></param>
        public static void LoadMultiRecord( ArrayList records , Stream recordsFile , int count )
        {
            int count1 = ( int )( ( recordsFile.Length - recordsFile.Position ) / Record.Sizeof( ) );
            if ( count1 > count )
            {
                count1 = count;
            }

            int length = count1 * Record.Sizeof( );
            byte[ ] ba = new byte[ length ];
            recordsFile.Read( ba , 0 , length );

            ArrayList list = Record.GetRecords( ba );
            records.AddRange( list.ToArray( ) );
        }

        /// <summary>
        /// 把一批记录写入文件中
        /// </summary>
        /// <param name="records"></param>
        /// <param name="recordsFileName"></param>
        public static void WriteRecordsToFile( ArrayList records , string recordsFileName )
        {
            FileStream recordsStream = null;

            //if (File.Exists(recordsFileName))
            {
                try
                {
                    recordsStream = new FileStream( recordsFileName , FileMode.Append , FileAccess.Write , FileShare.None );
                }
                catch ( IOException )
                {
                }
                catch ( SecurityException )
                {
                }
                catch ( UnauthorizedAccessException )
                {
                }
            }
            if ( recordsStream != null )
            {
                bool flag = false;
                try
                {
                    WriteRecordsToStream( records , recordsStream );
                }
                catch ( IOException )
                {
                }
                catch ( SecurityException )
                {
                }
                catch ( UnauthorizedAccessException )
                {
                }
                catch ( SerializationException ex )
                {
                    Console.WriteLine( ex.Message );
                    flag = true;
                }

                recordsStream.Close( );
            }
        }

        /// <summary>
        /// 把记录结构写入文件流
        /// </summary>
        /// <param name="records"></param>
        /// <param name="recordsStream"></param>
        public static void WriteRecordsToStream( ArrayList records , Stream recordsStream )
        {
            int size = Record.Sizeof( );
            int length = records.Count * Record.Sizeof( );
            byte[ ] ba = new byte[ length ];
            int pos = 0;
            int n = 0;
            while ( pos < length )
            {
                Record record = ( Record )records[ n++ ];
                record.ToByteArray( ba , ref pos );
            }
            recordsStream.Write( ba , 0 , length );
        }
    }
}